package com.landian.commons.jpa.utils;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;

public class PageUtils {

    /**
     * 假分页处理(有开源就用开源工具)
     * @param beanList 数据列表
     * @param pageable 分页上下文
     */
    public static<T> Page<T> fakePage(List<T> beanList, Pageable pageable){
        Long count = 0L;
        List<T> dataList = new ArrayList<>();
        if(null != beanList && beanList.size() > 0){
            Integer size = beanList.size();
            count = size.longValue();
            int start = pageable.getOffset();
            int pageLastIndex = start + pageable.getPageSize();
            for(int i = start; i<size && i< pageLastIndex; i++){
                dataList.add(beanList.get(i));
            }
        }
        return com.landian.commons.jpa.builder.PageImplBuilder.newInstance(pageable, dataList, count);
    }
}